package o;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import o.bwp.b;
import o.bwp.c;

final class bws implements bwr {
    private long ʽ;
    private fhz ˋ = fho.ˏ().ˋ("User");
    private long ˎ = 0;
    final bwp ˏ = c.ˎ(new b(this) {
        final /* synthetic */ bws ˊ;

        {
            this.ˊ = r1;
        }

        public void ˏ(int i) {
            bqx.ˏ("TotalMsgHelper", "get sns onResult: msgTotal = " + i);
            this.ˊ.ʻ();
            this.ˊ.ˏ(i);
        }
    });
    private Map<String, fkh> ॱ = Collections.synchronizedMap(new ds());
    private final Handler ॱॱ;
    private AtomicBoolean ᐝ = new AtomicBoolean(false);

    public bws() {
        ((cbd) this.ˋ.ॱ(cbd.class)).ˏ("TotalMsgHelper").ˏ(new fke<cbc>(this) {
            final /* synthetic */ bws ˊ;

            {
                this.ˊ = r1;
            }

            public /* synthetic */ void ॱ(Object obj) {
                ˎ((cbc) obj);
            }

            public void ˎ(fkg o_fkg) {
            }

            public void ˎ(cbc o_cbc) {
                long ॱ = (long) o_cbc.ॱ();
                bqx.ˊ("TotalMsgHelper", "onNext# update srvTotal:" + ॱ);
                this.ˊ.ˏ(ॱ);
                if (this.ˊ.ᐝ.get()) {
                    this.ˊ.ˎ = System.currentTimeMillis();
                    this.ˊ.ᐝ.set(false);
                    bqx.ˏ("TotalMsgHelper", "has finished get: " + this.ˊ.ᐝ.get());
                }
            }

            public void ˊ(Exception exception) {
                if (this.ˊ.ᐝ.get()) {
                    this.ˊ.ᐝ.set(false);
                }
                this.ˊ.ʼ();
            }

            public void ॱ() {
            }
        });
        HandlerThread handlerThread = new HandlerThread("GetMsgThread");
        handlerThread.start();
        Looper looper = handlerThread.getLooper();
        if (looper == null) {
            looper = Looper.getMainLooper();
        }
        this.ॱॱ = new Handler(looper);
    }

    public fkd<HashMap<String, String>> ˎ(@NonNull String str) {
        fkh ˋ = ˋ(str);
        ˋ();
        return ˋ.ˊ();
    }

    public void ˏ(long j) {
        this.ʽ = j;
        ʼ();
    }

    public void ˎ(final long j) {
        bqx.ˊ("TotalMsgHelper", "setCacheTotal:" + j);
        cbd o_cbd = (cbd) this.ˋ.ॱ(cbd.class);
        if (o_cbd == null) {
            bqx.ˊ("TotalMsgHelper", "the getPersonalInfo is null");
            return;
        }
        final cbc ˊ = o_cbd.ˊ();
        o_cbd.ˋ(new cbc(this) {
            final /* synthetic */ bws ˏ;

            public int ˎ() {
                return ˊ.ˎ();
            }

            public String ˏ() {
                return ˊ.ˏ();
            }

            public int ॱ() {
                return (int) j;
            }

            public int ˊ() {
                return ˊ.ˊ();
            }
        });
    }

    public void ॱ(long j) {
        bqx.ˊ("TotalMsgHelper", "setRead:" + j);
        this.ʽ -= j;
        ʼ();
    }

    public void ˎ() {
        bqx.ˊ("TotalMsgHelper", "setSNSMsgRead");
        ʼ();
    }

    public long ˊ() {
        return this.ʽ;
    }

    public void ॱ() {
        this.ʽ = 0;
        this.ˎ = 0;
        ʼ();
    }

    private fkh ˋ(final String str) {
        fkh o_fkh = (fkh) this.ॱ.get(str);
        if (o_fkh != null) {
            return o_fkh;
        }
        o_fkh = new fkh();
        this.ॱ.put(str, o_fkh);
        o_fkh.ॱ(new fkc(this) {
            final /* synthetic */ bws ˎ;

            public void ॱ() {
                this.ˎ.ॱ.remove(str);
            }
        });
        return o_fkh;
    }

    private void ˋ() {
        this.ॱॱ.postDelayed(new Runnable(this) {
            final /* synthetic */ bws ˎ;

            {
                this.ˎ = r1;
            }

            public void run() {
                bqx.ˊ("TotalMsgHelper", "getTotalMsg");
                if (this.ˎ.ʽ()) {
                    this.ˎ.ᐝ();
                } else {
                    this.ˎ.ॱॱ();
                }
            }
        }, 100);
    }

    private boolean ʽ() {
        return System.currentTimeMillis() - this.ˎ < 1800000 || this.ᐝ.get();
    }

    private void ᐝ() {
        bqx.ˊ("TotalMsgHelper", "getTotal from cache");
        ʼ();
    }

    private void ॱॱ() {
        bqx.ˊ("TotalMsgHelper", "getTotalFromServer");
        if (this.ˋ == null) {
            bqx.ˊ("TotalMsgHelper", "the User module is null");
            return;
        }
        cbd o_cbd = (cbd) this.ˋ.ॱ(cbd.class);
        if (o_cbd == null) {
            bqx.ˊ("TotalMsgHelper", "the getPersonalInfo is null");
            return;
        }
        this.ᐝ.set(true);
        o_cbd.ˏ();
    }

    private synchronized void ʼ() {
        if (bqy.ˋ().ˎ().equals(bqv.ˊ.ˎ())) {
            this.ˏ.ˊ();
        } else {
            ˏ(0);
        }
    }

    private void ʻ() {
        this.ˏ.ˋ();
    }

    private synchronized void ˏ(int i) {
        Set<Entry> entrySet = this.ॱ.entrySet();
        Object ˋ = ˋ(i);
        List<String> arrayList = new ArrayList();
        for (Entry entry : entrySet) {
            fkh o_fkh = (fkh) entry.getValue();
            if (o_fkh != null) {
                o_fkh.ˋ(ˋ);
            } else {
                arrayList.add(entry.getKey());
            }
        }
        if (!arrayList.isEmpty()) {
            for (String remove : arrayList) {
                this.ॱ.remove(remove);
            }
        }
    }

    private HashMap<String, String> ˋ(int i) {
        bqx.ˏ("TotalMsgHelper", "buildRetMsgInfo:mTotalMsg=" + this.ʽ + ",sysMsgCount=" + i);
        HashMap<String, String> hashMap = new HashMap();
        if (bqy.ˋ().ॱ() != bqv.ˎ) {
            hashMap.put(bqv.ˎ.ˎ(), ˋ(((long) i) + this.ʽ));
        }
        hashMap.put(bqy.ˋ().ॱ().ˎ(), ˋ(this.ʽ));
        return hashMap;
    }

    private static String ˋ(long j) {
        if (j >= 99) {
            return "99+";
        }
        if (j <= 0) {
            return "";
        }
        return String.valueOf(j);
    }

    public void ˏ() {
        this.ʽ = 0;
        this.ˎ = 0;
        ((cbd) this.ˋ.ॱ(cbd.class)).ˋ();
    }
}
